﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OnlineOrder.UI.用户窗体
{
    public partial class SocketClient : Form
    {
        public SocketClient()
        {
            InitializeComponent();
        }

        private System.Net.Sockets.Socket socket;

        private void btn_Join_Click(object sender, EventArgs e)
        {
            socket = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.IP);
            System.Net.IPAddress ip=System.Net.IPAddress.Parse(GetWlanIPv4Address());
            System.Net.IPEndPoint point=new System.Net.IPEndPoint(ip, int.Parse(this.txt_Port.Text));
            socket.Connect(point);
            System.Threading.Thread thread = new System.Threading.Thread(ReciveMsg);
            thread.IsBackground = true;
            thread.Start(socket);
        }
        static string GetWlanIPv4Address()
        {
            NetworkInterface wlanInterface = GetWlanInterface();

            if (wlanInterface != null)
            {
                foreach (UnicastIPAddressInformation ipAddressInfo in wlanInterface.GetIPProperties().UnicastAddresses)
                {
                    if (ipAddressInfo.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                    {
                        return ipAddressInfo.Address.ToString();
                    }
                }
            }

            return string.Empty;
        }

        static NetworkInterface GetWlanInterface()
        {
            NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();

            foreach (NetworkInterface networkInterface in interfaces)
            {
                if (networkInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 && networkInterface.OperationalStatus == OperationalStatus.Up)
                {
                    return networkInterface;
                }
            }

            return null;
        }

        private void ReciveMsg(object o)
        {
            System.Net.Sockets.Socket client=o as System.Net.Sockets.Socket;
            while (true)
            {
                try
                {
                    byte[] arrList = new byte[1024 * 1024];
                    int length = client.Receive(arrList);
                    this.txt_MessageBox.AppendText($"{DateTime.Now}：\r\n管理员：{Encoding.UTF8.GetString(arrList, 0, length)} \r\n");
                }catch (Exception)
                {
                    client.Close();
                }
            }
        }

        private void btn_Send_Click(object sender, EventArgs e)
        {
            if (txt_SendBox.Text != "")
            {
                byte[] arrMsg=Encoding.UTF8.GetBytes(txt_SendBox.Text);
                string msg=Encoding.UTF8.GetString(arrMsg);
                socket.Send(arrMsg);
                this.txt_SendBox.Text = " ";
                this.txt_MessageBox.AppendText($"{DateTime.Now}：\r\n用户：{msg} \r\n");
            }  
        }

        private void SocketClient_Load(object sender, EventArgs e)
        {
            var ip = GetWlanIPv4Address();
            this.txt_IPAddress.Text = ip;
            var port = 9999;
            this.txt_Port.Text = port.ToString();
        }
    }
}
